Page({
     formSubmit: function(e) {  
        var that = this;  
        var app=getApp();
        var formData = e.detail.value;  
        app.getUserInfo(function(res){ 
            //console.log(res)
            var openId=res;
            wx.request({  
            // url: 'http://246.ameiity.sinaapp.com/wxapp/php/regist.php', 
            url: 'https://ameiity.applinzi.com/wxapp/php/regist.php', 
            data: {
                'formdata':formData,
                'openid':openId
            },
            header: {  
                'Content-Type': 'application/json'  
            },  
            success: function(res) {  
                //console.log(res)
                //console.log(res.data)
                var sql_return=res.data.sql_return;

                if(sql_return == 4){
                    wx.showModal({
                        showCancel: false,
                        content: '学号或密码错误,请重试!'
                    });
                }

                if(sql_return == 10){
                    wx.showModal({
                        showCancel: false,
                        content: '教务系统崩溃拉.'
                    });
                }
                
                if(sql_return==1){
                    //console.log('绑定成功,并跳转到首页');
                    wx.setStorage({
                        key: 'isRegist',
                        data: true
                    })
                    wx.redirectTo({
                        url: '../index/index'
                    })
                    wx.showToast({
                        title: '绑定成功',
                        icon: 'success',
                        duration: 3000
                    });
                }else{
                    if(sql_return==3){
                        wx.showModal({
                            showCancel: false,
                            content: '学号格式错误,请重试'
                        });
                    }
                    if(sql_return==2){
                        //console.log('该微信号已绑定过学号，是否重新绑定');
                        wx.showModal({
                            title: '该微信号已绑定过学号，是否重新绑定',
                            confirmText: "是",
                            cancelText: "否",
                            success: function (res) {
                                //console.log(res);
                                if (res.confirm) {
                                    //console.log('用户点击是');
                                    wx.request({  
                                        // url: 'http://xiao.aviouy.cn/PHP/reregist.php', 
                                        url: 'https://ameiity.applinzi.com/wxapp/php/reregist.php', 
                                        data: {
                                            'formdata':formData,
                                            'openid':openId
                                        },
                                        header: {  
                                            'Content-Type': 'application/json'  
                                        },  
                                        success:function(res){
                                            //console.log(res)
                                            //console.log(res.data.sql_return);
                                            if(res.data.sql_return==1){
                                                wx.setStorage({
                                                    key: 'isRegist',
                                                    data: true
                                                })
                                                wx.navigateBack({
                                                    delta:1,
                                                    success:function(){
                                                        //wx.clearStorage() // 清楚本地缓存. xueqi信息等
                                                        wx.removeStorage({
                                                            key: 'xueqi'
                                                        })
                                                        wx.removeStorage({
                                                            key: 'kebiao'
                                                        })
                                                        wx.showToast({
                                                            title: '重新绑定成功',
                                                            icon: 'success',
                                                            duration: 1000
                                                        });
                                                    }
                                                })
                                            }else{
                                                //console.log('写入数据库失败,请检查sql语句')
                                                wx.showModal({
                                                    showCancel: false,
                                                    content: '学号格式错误,请重试'
                                                });
                                            }
                                        }
                                    })
                                }else{
                                    //console.log('跳转到课表页')
                                }
                            }
                        });
                        }
                //console.log('未知错误');
                }   
              }  
            })
     })  
    },  
});